//
//  SnowController.m
//  贝塞尔曲线
//
//  Created by 宜必鑫科技 on 2017/11/1.
//  Copyright © 2017年 宜必鑫科技. All rights reserved.
//
/**
 设置强制横屏 1.如果项目为横屏可以在General里面社会为横屏
 2.设置导航视图控制器为横屏的方法
 3.present 方式下面三局代码就好了 不过有点卡顿
 */
#import "SnowController.h"
#import <Masonry.h>
#import <ReactiveObjC.h>
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height

#define ThisWidth ScreenHeight
#define ThisHeight ScreenWidth
//通过RGB设置颜色
#define RGBColor(R,G,B)        [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:1.0]
@interface SnowController ()
@property(strong,nonatomic)CALayer *landLayer;
@property(strong,nonatomic)CAShapeLayer *greenTrack;
@property(strong,nonatomic)CAShapeLayer *yellowTrack;
@end

@implementation SnowController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
  
    /* 设置屏幕的渐变方向*/
    [self initBackGroundSky];
    
    /* 添加两座山 */
    [self initMountain];
    
    /* 初始化草坪 */
    [self initLawn];


    [self initYellowTrack];
    [self initGreenTrack];

    [self initTree];

    /* 初始化土地 */
    [self initLand];
    
    /* 延迟0.1s执行 */
    dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC));
    dispatch_after(delayTime, dispatch_get_main_queue(), ^{
        
        //    添加黄色轨道小车动画
        [self carAnimationWith:@"car" TrackLayer:_yellowTrack AnimationDuration:8.0 BeginTime:CACurrentMediaTime() + 1];
        //    添加绿色轨道小车动画
        [self carAnimationWith:@"otherCar" TrackLayer:_greenTrack AnimationDuration:5.0 BeginTime:CACurrentMediaTime()];
        
        [self initCloudAnimation];

    });


    NSLog(@"ScreenWidth = %f, self.view.size.weight = %f ",ScreenWidth,self.view.frame.size.width);
}

- (CAShapeLayer *)getLayerWithPath:(UIBezierPath *)path
{
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.frame = CGRectMake(0, 0, 200, 200);
    layer.position = (CGPoint){ScreenWidth/2,ScreenWidth/2};
    layer.fillColor = [UIColor clearColor].CGColor;
    layer.strokeColor = [UIColor orangeColor].CGColor;
    layer.backgroundColor = [UIColor whiteColor].CGColor;
    layer.path = path.CGPath;
    layer.lineWidth = 2;
    
    CABasicAnimation *checkAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    checkAnimation.duration =1.3f;
    checkAnimation.fromValue = [NSNumber numberWithFloat:0];
    checkAnimation.toValue = [NSNumber numberWithFloat:1.0];
    checkAnimation.fillMode =kCAFillModeForwards;
    [layer addAnimation:checkAnimation forKey:nil];
    
    return layer;
}


- (void)initBackGroundSky
{
    CAGradientLayer *backLayer = [[CAGradientLayer alloc]init];
    backLayer.frame = CGRectMake(0, 0, ScreenHeight, ScreenWidth);
    UIColor *lightColor = RGBColor(40, 150, 200);
    UIColor *darkColor = RGBColor(255, 255, 255);
    backLayer.colors = @[(__bridge id)lightColor.CGColor,(__bridge id)darkColor.CGColor];
    
    backLayer.startPoint = CGPointMake(0, 0);
    backLayer.endPoint = CGPointMake(1, 1);
    
    [self.view.layer addSublayer:backLayer];
}

- (void)initMountain
{
    /* 左面的雪山 */
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(0, ThisHeight - 20)];
    [path addLineToPoint:CGPointMake(100, 100)];
    [path addLineToPoint:CGPointMake(ThisWidth/2,  ThisHeight - 20)];
    [path closePath];
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.fillColor = [UIColor whiteColor].CGColor;
    layer.path = path.CGPath;
    [self.view.layer addSublayer:layer];
    
    CABasicAnimation *checkAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    checkAnimation.duration = 1.3f;
    checkAnimation.fromValue = [NSNumber numberWithFloat:0];
    checkAnimation.toValue = [NSNumber numberWithFloat:1.0];
    checkAnimation.fillMode =kCAFillModeForwards;
    [layer addAnimation:checkAnimation forKey:nil];
    
    /* 左面的颜色覆盖 */
    CGPoint leftMidddenPoint = CGPointMake(50, (ThisHeight - 20 + 100) / 2);
    CGPoint rightMidddenPoint = CGPointMake(ThisWidth/4 + 50, (ThisHeight - 20 + 100) / 2);
    UIBezierPath *pathBody = [UIBezierPath bezierPath];
    [pathBody moveToPoint:CGPointMake(0, ThisHeight - 20)];
    [pathBody addLineToPoint:leftMidddenPoint];
    [pathBody addLineToPoint:CGPointMake(100, (ThisHeight - 20 + 100) / 2 + 20)];
    [pathBody addLineToPoint:CGPointMake(150, (ThisHeight - 20 + 100) / 2 - 20)];
    [pathBody addLineToPoint:rightMidddenPoint];
    [pathBody addLineToPoint:CGPointMake(ThisWidth/2,  ThisHeight - 20)];
    [pathBody closePath];
    CAShapeLayer *layerBody = [CAShapeLayer layer];
    layerBody.fillColor = RGBColor(139, 92, 0).CGColor;
    layerBody.path = pathBody.CGPath;
    [self.view.layer addSublayer:layerBody];

    /* 右面的雪山 */
    UIBezierPath *rightpath = [UIBezierPath bezierPath];
    [rightpath moveToPoint:CGPointMake(ThisWidth/3, ThisHeight - 20)];
    [rightpath addLineToPoint:CGPointMake(ThisWidth/2, 200)];
    [rightpath addLineToPoint:CGPointMake(ThisWidth/1.2,  ThisHeight - 20)];
    [rightpath closePath];
    CAShapeLayer *rightlayer = [CAShapeLayer layer];
    rightlayer.fillColor = [UIColor whiteColor].CGColor;
    rightlayer.path = rightpath.CGPath;
    [self.view.layer addSublayer:rightlayer];

    /* 右面的颜色覆盖 */
    CGPoint leftBodyMidddenPoint = CGPointMake((ThisWidth/3 + ThisWidth/2)/2, (ThisHeight - 20 + 200) / 2);
    CGPoint rightBodyMidddenPoint = CGPointMake((ThisWidth/1.2 + ThisWidth/2)/2, (ThisHeight - 20 + 200) / 2);
    UIBezierPath *rightpathBody = [UIBezierPath bezierPath];
    [rightpathBody moveToPoint:CGPointMake(ThisWidth/3, ThisHeight - 20)];
    [rightpathBody addLineToPoint:leftBodyMidddenPoint];
    [rightpathBody addLineToPoint:CGPointMake((ThisWidth/3 + ThisWidth/2)/2 + 50, (ThisHeight - 20 + 200) / 2 + 20)];
    [rightpathBody addLineToPoint:CGPointMake((ThisWidth/3 + ThisWidth/2)/2 + 100, (ThisHeight - 20 + 200) / 2 - 20)];
    [rightpathBody addLineToPoint:rightBodyMidddenPoint];
    [rightpathBody addLineToPoint:CGPointMake(ThisWidth/1.2,  ThisHeight - 20)];
    [rightpathBody closePath];
    CAShapeLayer *rightlayerBody = [CAShapeLayer layer];
    rightlayerBody.fillColor = RGBColor(139, 92, 0).CGColor;
    rightlayerBody.path = rightpathBody.CGPath;
    [self.view.layer addSublayer:rightlayerBody];
}

- (void)initLawn
{
    /* 添加第一个草坪 */
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(0,  ThisHeight - 20)];
    [path addLineToPoint:CGPointMake(0,  ThisHeight - 100)];
    [path addQuadCurveToPoint:CGPointMake(ThisWidth/3, ThisHeight -20) controlPoint:CGPointMake(ThisWidth/5, ThisHeight - 100)];
    
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.fillColor = RGBColor(82, 177, 52).CGColor;
    layer.path = path.CGPath;
    [self.view.layer addSublayer:layer];
    
     /* 添加第二个草坪 */
    UIBezierPath *rightpath = [UIBezierPath bezierPath];
    [rightpath moveToPoint:CGPointMake(0,  ThisHeight - 20)];
    [rightpath addQuadCurveToPoint:CGPointMake(ThisWidth, ThisHeight - 100) controlPoint:CGPointMake(ThisWidth/5, ThisHeight - 100)];
    [rightpath addLineToPoint:CGPointMake(ThisWidth,  ThisHeight - 20)];

    
    CAShapeLayer *rightlayer = [CAShapeLayer layer];
    rightlayer.fillColor = RGBColor(92, 195, 52).CGColor;
    rightlayer.path = rightpath.CGPath;
    [self.view.layer addSublayer:rightlayer];
}


- (void)initLand
{
    CALayer *layer = [CALayer layer];
    layer.frame = CGRectMake(0, ThisHeight - 20, ThisWidth, 20);
    layer.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"ground"]].CGColor;
    [self.view.layer addSublayer:layer];
    _landLayer = layer;
}

- (void)initYellowTrack
{
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(0, ThisHeight - 50)];
    [path addCurveToPoint:CGPointMake(ThisWidth/1.5, ThisHeight/2 - 20) controlPoint1:CGPointMake(ThisWidth / 6.0, ThisHeight - 200) controlPoint2:CGPointMake(ThisWidth / 3.0, ThisHeight + 50)];
    [path addQuadCurveToPoint:CGPointMake(ThisWidth + 50, ThisHeight / 3.0f) controlPoint:CGPointMake(ThisWidth - 100, 50)];
//    [path addLineToPoint:CGPointMake(ThisWidth + 10 ,ThisHeight + 10)];
    [path addLineToPoint:CGPointMake(ThisWidth, ThisHeight - 50)];
    
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.path = path.CGPath;
    layer.lineWidth = 5;
    layer.strokeColor = [UIColor colorWithDisplayP3Red:210.0 / 255.0 green:179.0 / 255.0 blue:54.0 / 255.0 alpha:1.0].CGColor;
    layer.fillColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yellow"]].CGColor;
    [self.view.layer addSublayer:layer];
    _yellowTrack = layer;
    //    为了能够让弧线更好看一点，需要加入镂空的虚线
    CAShapeLayer *trackLine = [[CAShapeLayer alloc] init];
    trackLine.lineCap = kCALineCapRound;
    trackLine.strokeColor = [UIColor whiteColor].CGColor;
    
    trackLine.lineDashPattern = @[@1.0,@6.0];
    trackLine.lineWidth = 2.5;
    trackLine.fillColor = [UIColor clearColor].CGColor;
    trackLine.path = path.CGPath;
    [layer addSublayer:trackLine];
}

- (void)initGreenTrack
{
    //    绿色铁轨的火车从右侧进入，所以从右侧开始绘画。需要画三条曲线，右边一条+中间的圆圈+左边一条
    UIBezierPath *path = [[UIBezierPath alloc] init];
    [path moveToPoint:CGPointMake(ThisWidth + 10, ThisHeight)];
    [path addLineToPoint:CGPointMake(ThisWidth + 10,ThisHeight - 70)];
    [path addQuadCurveToPoint:CGPointMake(ThisWidth / 1.5, ThisHeight - 70) controlPoint:CGPointMake(ThisWidth - 150, 200)];
    
    //    画圆圈
    [path addArcWithCenter:CGPointMake(ThisWidth / 1.6, ThisHeight - 140) radius:70 startAngle:M_PI_2 endAngle:2.5 * M_PI clockwise:YES];
    [path addCurveToPoint:CGPointMake(0,ThisHeight - 100) controlPoint1:CGPointMake(ThisWidth / 1.8 - 60, ThisHeight - 60) controlPoint2:CGPointMake(150, ThisHeight / 2.3)];
    [path addLineToPoint:CGPointMake(- 10, ThisHeight)];
    
    _greenTrack = [[CAShapeLayer alloc] init];
    _greenTrack.lineWidth = 5;
    _greenTrack.strokeColor = [UIColor colorWithDisplayP3Red:0.0 / 255.0 green:147.0 / 255.0 blue:163.0 /255.0  alpha:1.0].CGColor;

    _greenTrack.path = path.CGPath;
    _greenTrack.fillColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"green"]].CGColor;
    [self.view.layer addSublayer:_greenTrack];
    
    //    为了能够让弧线更好看一点，需要加入镂空的虚线
    CAShapeLayer *trackLine = [[CAShapeLayer alloc] init];
    trackLine.lineCap = kCALineCapRound;
    trackLine.strokeColor = [UIColor whiteColor].CGColor;
    
    trackLine.lineDashPattern = @[@1.0,@6.0];
    trackLine.lineWidth = 2.5;
    trackLine.fillColor = [UIColor clearColor].CGColor;
    trackLine.path = path.CGPath;
    [_greenTrack addSublayer:trackLine];
}

//添加点缀的小树
- (void)initTree{
    
    [self addTreesWithNumber:7 treeFrame:CGRectMake(0, ThisHeight - 20, 13, 23)];
    [self addTreesWithNumber:7 treeFrame:CGRectMake(0, ThisHeight - 64, 18, 32)];
    [self addTreesWithNumber:5 treeFrame:CGRectMake(0, ThisHeight - 90, 13, 23)];
}

////添加小树
- (void)addTreesWithNumber:(NSInteger)treesNumber treeFrame:(CGRect)frame
{
    UIImage *tree = [UIImage imageNamed:@"tree"];
    for (NSInteger i = 0; i < treesNumber + 1; i++)
    {
        CALayer *treeLayer = [[CALayer alloc] init];
        treeLayer.contents = (__bridge id _Nullable)(tree.CGImage);
        treeLayer.frame = CGRectMake(ThisWidth - 50 * i * (arc4random_uniform(4) + 1), frame.origin.y, frame.size.width, frame.size.height);
        [self.view.layer insertSublayer:treeLayer above:_greenTrack];
    }
}
//云彩的动画
- (void)initCloudAnimation
{
    CALayer *cloud = [[CALayer alloc]init];
    cloud.contents = (__bridge id _Nullable)([UIImage imageNamed:@"cloud"].CGImage);
    cloud.frame = CGRectMake(0, 50, 63, 20);
    [self.view.layer addSublayer:cloud];
    
    UIBezierPath *cloudPath = [[UIBezierPath alloc] init];
    [cloudPath moveToPoint:CGPointMake(ThisWidth + 63, 50)];
    [cloudPath addLineToPoint:CGPointMake(-63, 50)];
    
    CAKeyframeAnimation *ani = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    ani.path = cloudPath.CGPath;
    ani.duration = 30;
    ani.autoreverses = NO;
    ani.repeatCount = CGFLOAT_MAX;
    ani.calculationMode = kCAAnimationPaced;
    
    [cloud addAnimation:ani forKey:@"position"];
}


//抽取过山车的动画
- (CAKeyframeAnimation *)carAnimationWith:(NSString *)carImageName TrackLayer:(CAShapeLayer *)track AnimationDuration:(CFTimeInterval)duration BeginTime:(CFTimeInterval)beginTime{
    CALayer *car = [[CALayer alloc] init];
    car.frame = CGRectMake(0, 0, 22, 15);
    car.contents = (__bridge id _Nullable)([UIImage imageNamed:carImageName].CGImage);
    
    CAKeyframeAnimation *ani = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    ani.path = track.path;
    
    ani.duration = duration;
    ani.beginTime = beginTime;
    ani.autoreverses = NO;
    ani.repeatCount = CGFLOAT_MAX;
    ani.calculationMode = kCAAnimationPaced;
    ani.rotationMode = kCAAnimationRotateAuto;
    
    [track addSublayer:car];
    [car addAnimation:ani forKey:@"carAni"];
    
    return ani;
}


- (BOOL)shouldAutorotate{
    return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskLandscapeLeft;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationLandscapeLeft;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];

    [UIView animateWithDuration:0.2f animations:^{
        self.view.frame = CGRectMake(0, ScreenHeight, ScreenWidth, ScreenHeight);
        self.view.alpha = 0.0f;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:nil];
    }];
}

@end
